package com.yx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.common.Const;
import com.yx.controller.BaseServlet;
import com.yx.dao.PersonMapper;
import com.yx.entity.Person;
import com.yx.util.DateUtils;
import com.yx.util.MybatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Classname PersonController
 * @Description TODO:
 * @Date 2021/05/14 19:34
 * @Created by yx
 */
@WebServlet("/person")
public class PersonController extends BaseServlet {

    public String getAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer pageNum = null;
        String num = request.getParameter("pageNum");
        if (num==null){
            pageNum = 1;
        }else {
            pageNum=Integer.valueOf(request.getParameter("pageNum"));
        }
        PageHelper.startPage(pageNum,5);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        List<Person> personList = mapper.selectAll();
        PageInfo<Person> pageInfo = new PageInfo<Person>(personList);
//        MybatisUtils.closeSession();
        System.out.println(pageInfo);
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(pageInfo);
        return string;
    }

    public String register(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("username");
        Integer age = Integer.valueOf(request.getParameter("age"));
        String password = request.getParameter("password");
        java.sql.Date bornDate = DateUtils.dataToSql(DateUtils.stringToDate(request.getParameter("bornDate")));
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Person person = new Person(name, age, password, bornDate, email, address);
        System.out.println(person);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Integer integer = mapper.insertPerson(person);
        MybatisUtils.commit();
        if (integer > 0) {
            return Const.SUCCESS;
        } else {
            return Const.FAIL;
        }

    }

    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + "<========username=========");
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Person login = mapper.login(username, password);
        System.out.println(login);
//        MybatisUtils.closeSession();
        if (login != null) {
            return Const.SUCCESS;
        } else {
            return Const.FAIL;
        }
    }

    public String deletePerson(HttpServletRequest request, HttpServletResponse response){
        Integer id = Integer.valueOf(request.getParameter("id"));
        System.out.println("======id======>"+id);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Integer integer = mapper.deletePerson(id);
        MybatisUtils.commit();
        if (integer>0){
            return Const.SUCCESS;
        }else {
            return Const.FAIL;
        }
    }

    public String addPerson(HttpServletRequest request, HttpServletResponse response){
//       return register(request,response);
        String name = request.getParameter("username");
        Integer age = Integer.valueOf(request.getParameter("age"));
        String password = request.getParameter("password");
        java.sql.Date bornDate = DateUtils.dataToSql(DateUtils.stringToDate(request.getParameter("bornDate")));
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Person person = new Person(name, age, password, bornDate, email, address);
        System.out.println(person);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Integer integer = mapper.insertPerson(person);
        MybatisUtils.commit();
        MybatisUtils.closeSession();
        if (integer > 0) {
            return Const.SUCCESS;
        } else {
            return Const.FAIL;
        }
    }

    public String getPersonById(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Person personById = mapper.getPersonById(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(personById);
        return string;
    }

    public String updatePerson(HttpServletRequest request, HttpServletResponse response){
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("username");
        Integer age = Integer.valueOf(request.getParameter("age"));
        String password = request.getParameter("password");
        java.sql.Date bornDate = DateUtils.dataToSql(DateUtils.stringToDate(request.getParameter("bornDate")));
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Person person = new Person(id,name, age, password, bornDate, email, address);
        System.out.println(person);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Integer integer = mapper.updatePerson(person);
        MybatisUtils.commit();
        MybatisUtils.closeSession();
        if (integer>0){
            return Const.SUCCESS;
        }else {
            return Const.FAIL;
        }
    }

}
